﻿using IPR_StudyMeetingMrgSys.Enums;
using IPR_StudyMeetingMrgSys.Extension;
using IPR_StudyMeetingMrgSys.Models;
using IPR_StudyMeetingMrgSys.ViewModel;
using IPR_StudyMeetingMrgSys.Common;

using log4net;
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Reflection;
using System.Web;
using System.Web.Http;

namespace IPR_StudyMeetingMrgSys.Controllers
{
    /// <summary>
    /// 2019-11-30 经过修改 该接口不使用，转入 TrainPlanAndContentStudyRateController
    /// </summary>
    public class TrainCourseStudyUserController : ApiController
    {
        ILog log = log4net.LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType);
        [HttpGet]
        [Route("api/TrainCourseStudyUser/GetTrainCourseStudyUser")]
        public IHttpActionResult GetTrainCourseStudyUser(string swhere, int page, int limit)
        {
            string TPID = "";
            string searchLevel = "";
            if (!string.IsNullOrEmpty(swhere == null ? string.Empty : swhere.Trim()))
            {
                Dictionary<string, string> jsonObject = JsonConvert.DeserializeObject<Dictionary<string, string>>(swhere);
                swhere = string.Empty;
                foreach (var item in jsonObject)
                {
                    if (item.Key == "TPID")
                    {
                        if (item.Value != "")
                            TPID = item.Value;
                    }
                    if (item.Key == "searchLevel")
                    {
                        if (item.Value != "")
                            searchLevel = item.Value;
                    }
                }
            }

            ResultMsg resultMsg = new ResultMsg();
            int startindex = (page - 1) * limit + 1;
            int endindex = page * limit;
            string strSql = "";
            string strSqlpage = "";
            try
            {
                if ( String.IsNullOrEmpty( TPID ))
                {
                    resultMsg.StatusCode = (int)StatusCodeEnum.Success;
                    resultMsg.Info = "请选择计划";
                    resultMsg.Data = null;
                }
                else if (searchLevel == "1")
                {
                    //--统计车间人员总数量
                    strSql = "select * from  ( SELECT ROW_NUMBER() OVER(order by TPName) as Row, ";
                    strSql += "  TPName, DepPID AS DeptID, DepPName as DeptName,ISNULL( COUNT(UserID),0) as needtrainnum , ISNULL( COUNT(studyuserid),0) as studyusernum,IsEnd,ISNULL( COUNT(studyuserid),0)*100/ ISNULL( COUNT(UserID),0) AS rate from " +
                        " V_CourseStudyUserInfo " +
                        " where TPID =  " + TPID;
                    //要查询的是车间级别的统计(单击的是车间列表表头)
                    if (LoginUser.DepLevel == 0)
                    {
                        //如果当前登录部门级别是段级别，查询所有车间的统计
                    }
                    else if (LoginUser.DepLevel == 1)
                    {
                        //如果当前登录部门级别是车间级别，查询当前车间的统计
                        strSql += " and DepPID=" + LoginUser.DepID.ToString();
                    }
                    else if (LoginUser.DepLevel == 2)
                    {
                        //如果当前登录部门级别是车队没有权限
                        resultMsg.Info = "您没有权限";
                    }
                    strSql += " group by TPName, DepPID,DepPName,IsEnd ";
                    strSql += " ) TT";
                    strSqlpage = strSql + String.Format(" WHERE TT.Row between {0} and {1}", startindex, endindex);

                }
                else if (searchLevel == "2")
                {
                    //--统计车队人员总数量
                    strSql = "select * from  ( SELECT ROW_NUMBER() OVER(order by TPName) as Row, ";
                    strSql += " TPName, DeptID AS DeptID, DepName as DeptName,ISNULL( COUNT(UserID),0) as needtrainnum , ISNULL( COUNT(studyuserid),0) as studyusernum,IsEnd,ISNULL( COUNT(studyuserid),0)*100/ ISNULL( COUNT(UserID),0) AS rate from   V_CourseStudyUserInfo  where TPID = " + TPID.ToString();
                    //统计车队级别的人员学习次数
                    //要查询的是车间级别的统计(单击的是车间列表表头)
                    if (LoginUser.DepLevel == 0)
                    {

                    }
                    else if (LoginUser.DepLevel == 1)
                    {
                        //如果当前登录部门级别是车间级别，查询当前车间的统计
                        strSql += " and DepPID=" + LoginUser.DepID.ToString();
                    }
                    else if (LoginUser.DepLevel == 2)
                    {
                        //如果当前登录部门级别是车队,查询当前车队汇总统计
                        strSql += " and DeptID=" + LoginUser.DepID.ToString();
                    }
                    strSql += " group by TPName, DeptID,DepName,IsEnd ";
                    strSql += " ) TT";
                    strSqlpage = strSql + String.Format(" WHERE TT.Row between {0} and {1}", startindex, endindex);
                }
                else if (searchLevel == "3")
                {
                    //乘务员级别的学习次数统计

                    strSql = "select * from  ( SELECT ROW_NUMBER() OVER(order by TPName) as Row, ";
                    strSql += "	TPName, UserID AS DeptID,UserName as DeptName,ISNULL( COUNT(UserID),0) as needtrainnum , ISNULL( COUNT(studyuserid),0) as studyusernum,IsEnd,ISNULL( COUNT(studyuserid),0)*100/ ISNULL( COUNT(UserID),0) AS rate from   V_CourseStudyUserInfo where TPID=" + TPID.ToString();
                    //要查询的是车间级别的统计(单击的是车间列表表头)
                    if (LoginUser.DepLevel == 0)
                    {
                        //如果当前登录部门级别是段级别，查询所有车间的统计
                    }
                    else if (LoginUser.DepLevel == 1)
                    {
                        //如果当前登录部门级别是车间级别，查询当前车间的统计
                        strSql += " and DepPID=" + LoginUser.DepID.ToString();
                    }
                    else if (LoginUser.DepLevel == 2)
                    {
                        //如果当前登录部门级别是车队,查询当前车队汇总统计
                        strSql += " and DeptID=" + LoginUser.DepID.ToString();
                    }
                    strSql += "group by  TPName, UserID,UserName,IsEnd ";
                    strSql += " ) TT";
                    strSqlpage = strSql + String.Format(" WHERE TT.Row between {0} and {1}", startindex, endindex);

                }
                if (resultMsg.Info != "" && resultMsg.Info != null)
                {
                    //返回token信息
                    resultMsg.StatusCode = (int)StatusCodeEnum.Unauthorized;
                    resultMsg.Info = "您没有权限或计划未选择！";
                    resultMsg.Data = null;
                }
                else
                {
                    int rowcount = 0;
                    DataSet dscount = new IPR_StudyMeetingMrgSys.BLL.V_CourseStudyUserInfo().GetAllList(strSql);
                    if (dscount != null)
                        if (dscount.Tables.Count > 0)
                            if (dscount.Tables[0].Rows.Count > 0)
                            {
                                rowcount = dscount.Tables[0].Rows.Count;
                            }
                    bool blExist = false;
                    DataSet dsSearch = new IPR_StudyMeetingMrgSys.BLL.V_CourseStudyUserInfo().GetAllList(strSqlpage);
                    if (dsSearch != null)
                        if (dsSearch.Tables.Count > 0)
                            if (dsSearch.Tables[0].Rows.Count > 0)
                            {
                                //返回token信息
                                resultMsg.StatusCode = (int)StatusCodeEnum.Success;
                                resultMsg.Info = StatusCodeEnum.Success.GetEnumText();
                                resultMsg.Data = dsSearch.Tables[0];
                                resultMsg.Count = rowcount;
                                blExist = true;
                            }
                    if (!blExist)
                    {
                        //返回token信息
                        resultMsg.StatusCode = (int)StatusCodeEnum.Success;
                        resultMsg.Info = "查询结果不存在";
                        resultMsg.Data = null;
                    }
                }
            }
            catch (Exception exc)
            {
                resultMsg.StatusCode = (int)StatusCodeEnum.Error;
                resultMsg.Info = StatusCodeEnum.Error.GetEnumText() + exc.Message;
                resultMsg.Data = "查询异常！" + exc.Message;
            }
            return Json(resultMsg);
        }


        [HttpGet]
        [Route("api/TrainCourseStudyUser/GetTrainContentStudyUser")]
        public IHttpActionResult GetTrainContentStudyUser(string swhere, int page, int limit)
        {
            string TPID = "";
            string TContent = "";
            string searchLevel = "";
            if (!string.IsNullOrEmpty(swhere == null ? string.Empty : swhere.Trim()))
            {
                Dictionary<string, string> jsonObject = JsonConvert.DeserializeObject<Dictionary<string, string>>(swhere);
                swhere = string.Empty;
                foreach (var item in jsonObject)
                {
                    if (item.Key == "TPID")
                    {
                        if (item.Value != "")
                            TPID = item.Value;
                    }
                    if (item.Key == "TContent")
                    {
                        if (item.Value != "")
                            TContent = item.Value;
                    }
                    if (item.Key == "searchLevel")
                    {
                        if (item.Value != "")
                            searchLevel = item.Value;
                    }
                }
            }

            ResultMsg resultMsg = new ResultMsg();
            int startindex = (page - 1) * limit + 1;
            int endindex = page * limit;
            string strSql = "";
            string strSqlpage = "";
            try
            {
                if (String.IsNullOrEmpty(TContent))
                {
                    resultMsg.StatusCode = (int)StatusCodeEnum.Success;
                    resultMsg.Info = "请选择内容";
                    resultMsg.Data = null;
                }
                else if (searchLevel == "1")
                {
                    //--统计车间人员总数量
                    strSql = "select * from  ( SELECT ROW_NUMBER() OVER(order by TPName) as Row, TPName,TCOName,TContent,DeptID,DeptName,needtrainnum,studyusernum,IsEnd,rate from (";
                    strSql += " select TPName,TCOName,TContent, DepPID AS DeptID, DepPName as DeptName,ISNULL( COUNT(UserID),0) as needtrainnum , ISNULL( COUNT(studyuserid),0) as studyusernum,IsEnd,ISNULL( COUNT(studyuserid),0)*100/ ISNULL( COUNT(UserID),0) AS rate  from " +
                        " V_ContentStudyUserInfo " +
                        " where 1=1 ";
                    if (!string.IsNullOrEmpty(TPID))
                        strSql += " and TPID =  " + TPID;
                    if (!string.IsNullOrEmpty(TContent))
                        strSql += " and TContent =  '" + TContent+"'";
                    //要查询的是车间级别的统计(单击的是车间列表表头)
                    if (LoginUser.DepLevel == 0)
                    {
                        //如果当前登录部门级别是段级别，查询所有车间的统计
                    }
                    else if (LoginUser.DepLevel == 1)
                    {
                        //如果当前登录部门级别是车间级别，查询当前车间的统计
                        strSql += " and DepPID=" + LoginUser.DepID.ToString();
                    }
                    else if (LoginUser.DepLevel == 2)
                    {
                        //如果当前登录部门级别是车队没有权限
                        resultMsg.Info = "您没有权限";
                    }
                    strSql += " group by TPName,TCOName,TContent,DepPID,DepPName,IsEnd ";
                    strSql += " ) T1";
                    strSql += " ) TT";
                    strSqlpage = strSql + String.Format(" WHERE TT.Row between {0} and {1}", startindex, endindex);

                }
                else if (searchLevel == "2")
                {
                    //--统计车队人员总数量
                    strSql = "select * from  ( SELECT ROW_NUMBER() OVER(order by TPName) as Row, TPName,TCOName,TContent,DeptID,DeptName,needtrainnum,studyusernum,IsEnd,rate from (";
                    strSql += " select TPName,TCOName,TContent, DeptID AS DeptID, DepName as DeptName,ISNULL( COUNT(UserID),0) as needtrainnum , ISNULL( COUNT(studyuserid),0) as studyusernum,IsEnd,ISNULL( COUNT(studyuserid),0)*100/ ISNULL( COUNT(UserID),0) AS rate  from   V_ContentStudyUserInfo  where 1=1 ";
                    if (!string.IsNullOrEmpty(TPID))
                        strSql += " and TPID =  " + TPID;
                    if (!string.IsNullOrEmpty(TContent))
                        strSql += " and TContent =  '" + TContent + "'";
                    //统计车队级别的人员学习次数
                    //要查询的是车间级别的统计(单击的是车间列表表头)
                    if (LoginUser.DepLevel == 0)
                    {

                    }
                    else if (LoginUser.DepLevel == 1)
                    {
                        //如果当前登录部门级别是车间级别，查询当前车间的统计
                        strSql += " and DepPID=" + LoginUser.DepID.ToString();
                    }
                    else if (LoginUser.DepLevel == 2)
                    {
                        //如果当前登录部门级别是车队,查询当前车队汇总统计
                        strSql += " and DeptID=" + LoginUser.DepID.ToString();
                    }
                    strSql += " group by TPName,TCOName,TContent,DeptID,DepName,IsEnd ";
                    strSql += " ) T1";
                    strSql += " ) TT";
                    strSqlpage = strSql + String.Format(" WHERE TT.Row between {0} and {1}", startindex, endindex);
                }
                else if (searchLevel == "3")
                {
                    //乘务员级别的学习次数统计

                    strSql = "select * from  ( SELECT ROW_NUMBER() OVER(order by TPName) as Row, TPName,TCOName,TContent,DeptID,DeptName,needtrainnum,studyusernum,IsEnd,rate from (";
                    strSql += "select	TPName,TCOName,TContent, UserID AS DeptID,UserName as DeptName,ISNULL( COUNT(UserID),0) as needtrainnum , ISNULL( COUNT(studyuserid),0) as studyusernum,IsEnd,ISNULL( COUNT(studyuserid),0)*100/ ISNULL( COUNT(UserID),0) AS rate from   V_ContentStudyUserInfo where 1=1";
                    if (!string.IsNullOrEmpty(TPID))
                        strSql += " and TPID =  " + TPID;
                    if (!string.IsNullOrEmpty(TContent))
                        strSql += " and TContent =  '" + TContent + "'";
                    //要查询的是车间级别的统计(单击的是车间列表表头)
                    if (LoginUser.DepLevel == 0)
                    {
                        //如果当前登录部门级别是段级别，查询所有车间的统计
                    }
                    else if (LoginUser.DepLevel == 1)
                    {
                        //如果当前登录部门级别是车间级别，查询当前车间的统计
                        strSql += " and DepPID=" + LoginUser.DepID.ToString();
                    }
                    else if (LoginUser.DepLevel == 2)
                    {
                        //如果当前登录部门级别是车队,查询当前车队汇总统计
                        strSql += " and DeptID=" + LoginUser.DepID.ToString();
                    }
                    strSql += " group by TPName,TCOName,TContent, UserID ,UserName,IsEnd ";
                    strSql += " ) T1";
                    strSql += " ) TT";
                    strSqlpage = strSql + String.Format(" WHERE TT.Row between {0} and {1}", startindex, endindex);

                }
                if (resultMsg.Info != "" && resultMsg.Info != null)
                {
                    //返回token信息
                    resultMsg.StatusCode = (int)StatusCodeEnum.Unauthorized;
                    resultMsg.Info = "您没有权限或计划未选择！";
                    resultMsg.Data = null;
                }
                else
                {
                    int rowcount = 0;
                    DataSet dscount = new IPR_StudyMeetingMrgSys.BLL.V_CourseStudyUserInfo().GetAllList(strSql);
                    if (dscount != null)
                        if (dscount.Tables.Count > 0)
                            if (dscount.Tables[0].Rows.Count > 0)
                            {
                                rowcount = dscount.Tables[0].Rows.Count;
                            }
                    bool blExist = false;
                    DataSet dsSearch = new IPR_StudyMeetingMrgSys.BLL.V_CourseStudyUserInfo().GetAllList(strSqlpage);
                    if (dsSearch != null)
                        if (dsSearch.Tables.Count > 0)
                            if (dsSearch.Tables[0].Rows.Count > 0)
                            {
                                //返回token信息
                                resultMsg.StatusCode = (int)StatusCodeEnum.Success;
                                resultMsg.Info = StatusCodeEnum.Success.GetEnumText();
                                resultMsg.Data = dsSearch.Tables[0];
                                resultMsg.Count = rowcount;
                                blExist = true;
                            }
                    if (!blExist)
                    {
                        //返回token信息
                        resultMsg.StatusCode = (int)StatusCodeEnum.Success;
                        resultMsg.Info = "查询结果不存在";
                        resultMsg.Data = null;
                    }
                }
            }
            catch (Exception exc)
            {
                resultMsg.StatusCode = (int)StatusCodeEnum.Error;
                resultMsg.Info = StatusCodeEnum.Error.GetEnumText() + exc.Message;
                resultMsg.Data = "查询异常！" + exc.Message;
            }
            return Json(resultMsg);
        }



    }
}
